package com.hsgene.product.service.impl;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.util.qiniu.QiniuCertificateUtil;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.generator.dto.GeneratorDto;
import com.hsgene.product.condition.PackageCondition;
import com.hsgene.product.condition.ReportTemplateCondition;
import com.hsgene.product.constants.PolicyType;
import com.hsgene.product.domain.GeneticTestingPackage;
import com.hsgene.product.domain.GeneticTestingProduct;
import com.hsgene.product.domain.GeneticTestingReportTemplate;
import com.hsgene.product.dto.GeneticTestingPackageDto;
import com.hsgene.product.dto.GeneticTestingReportTemplateDto;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.persistence.GeneticTestingPackageRepository;
import com.hsgene.product.persistence.GeneticTestingReportTemplateMapper;
import com.hsgene.product.service.ReportTemplateService;
import com.hsgene.product.service.api.AgencyService;
import com.hsgene.product.service.api.GeneratorService;
import com.hsgene.supplier.dto.GeneticTestingAgencyDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: //TODO
 * @ProjectName: hdas-geneshop-server
 * @Package: com.hsgene.product.service.impl
 * @Author: FX
 * @CreateDate: 2018/11/5 11:40
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class ReportTemplateServiceImpl implements ReportTemplateService {
    @Autowired
    GeneticTestingReportTemplateMapper geneticTestingReportTemplateMapper;
    @Autowired
    GeneratorService generatorService;
    @Autowired
    GeneticTestingPackageRepository geneticTestingPackageRepository;
    @Autowired
    AgencyService agencyService;
    @Override
    public ApiResult addReportTemplate(GeneticTestingReportTemplate geneticTestingReportTemplate) {
        geneticTestingReportTemplate.check();
        GeneticTestingReportTemplate testingReportTemplate=geneticTestingReportTemplateMapper.getByPackageIdAndAgenceId(geneticTestingReportTemplate);
        if(testingReportTemplate!=null){
          return ApiResult.fail(ProductErrorCode.EXISTING_TEMPLATE_CANNOT_BE_ADDED);
        }
        ApiResult<GeneratorDto> generator = generatorService.getGenerator(null, null);
        if(!generator.isSucc()){
            return ApiResult.fail(generator.getError());
        }
        geneticTestingReportTemplate.setId(generator.getData().getSerialNumber());
        geneticTestingReportTemplate.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
        geneticTestingReportTemplate.setCreateDateTime(new Date());
        geneticTestingReportTemplate.setUpdateDateTime(geneticTestingReportTemplate.getCreateDateTime());
        geneticTestingReportTemplateMapper.insertSelective(geneticTestingReportTemplate);
        return ApiResult.succ();
    }

    @Override
    public ApiResult updateReportTemplate(GeneticTestingReportTemplate geneticTestingReportTemplate) {
        if(StringUtils.isEmpty(geneticTestingReportTemplate.getPdfUrl())){
            return ApiResult.fail(ProductErrorCode.PARAMETER_CANNOT_BE_EMPTY);
        }
        geneticTestingReportTemplate.setUpdateDateTime(new Date());
        geneticTestingReportTemplateMapper.updateByPrimaryKeySelective(geneticTestingReportTemplate);
        return ApiResult.succ();
    }



    @Override
    public ApiResult<Pagination<GeneticTestingReportTemplateDto>> getList(ReportTemplateCondition condition) {
        int totalCount=geneticTestingReportTemplateMapper.getListCount(condition);
        Pagination<GeneticTestingReportTemplateDto> pagination = Pagination.newInstance(condition.getLimitHelper(), totalCount);
        int rowNumber = (condition.getLimitHelper().getPage() - 1) * condition.getLimitHelper().getSize();
        if(totalCount>0){
            List<GeneticTestingReportTemplateDto> packageList=geneticTestingReportTemplateMapper.getList(condition);
            for (int i = 0, n = packageList.size(); i < n; i++) {
                GeneticTestingReportTemplateDto geneticTestingReportTemplateDto = packageList.get(i);
                geneticTestingReportTemplateDto.setNumber(rowNumber + i + 1);
            }
            pagination.setResult(packageList);
        }else{
            pagination.setResult(new ArrayList<>());
        }
        return ApiResult.succ(pagination);
    }

    @Override
    public ApiResult deleteTeplete(String id) {
        GeneticTestingReportTemplate testingReportTemplate = new GeneticTestingReportTemplate();
        testingReportTemplate.setId(id);
        testingReportTemplate.setDeleteFlag(DeleteFlagType.DELETE.getCode());
        geneticTestingReportTemplateMapper.updateByPrimaryKeySelective(testingReportTemplate);
        return ApiResult.succ();
    }

    @Override
    public ApiResult<GeneticTestingReportTemplateDto> getById(String id) {
        GeneticTestingReportTemplate testingReportTemplate = geneticTestingReportTemplateMapper.selectByPrimaryKey(id);
        GeneticTestingPackage testingPackage = geneticTestingPackageRepository.selectByPrimaryKey(testingReportTemplate.getPackageId());
        ApiResult<GeneticTestingAgencyDto> agencyById = agencyService.getAgencyById(testingReportTemplate.getAgencyId());
        if(!agencyById.isSucc()){
            return ApiResult.fail(agencyById.getError());
        }
        GeneticTestingReportTemplateDto geneticTestingReportTemplateDto = new GeneticTestingReportTemplateDto();
        BeanUtils.copyProperties(testingReportTemplate,geneticTestingReportTemplateDto);
        geneticTestingReportTemplateDto.setPackageName(testingPackage.getPackageName());
        geneticTestingReportTemplateDto.setAgencyName(agencyById.getData().getName());
        String downloadCertificateStr = QiniuCertificateUtil.getDownloadCertificateStr(geneticTestingReportTemplateDto.getPdfUrl());
        geneticTestingReportTemplateDto.setPdfUrlScan(downloadCertificateStr);
        return ApiResult.succ(geneticTestingReportTemplateDto);
    }

    @Override
    public ApiResult deleteTepleteMore(List<GeneticTestingReportTemplate> testingReportTemplates) {
        geneticTestingReportTemplateMapper.deleteTepleteMore(testingReportTemplates);
        return ApiResult.succ();
    }

    @Override
    public ApiResult<List<GeneticTestingReportTemplateDto>> getListPean(ReportTemplateCondition condition) {
        List<GeneticTestingReportTemplateDto> list= geneticTestingReportTemplateMapper.getListPean(condition);
        return ApiResult.succ(list);
    }
}
